package controllers

import (
	"encoding/json"
	"fmt"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type AllocationController struct {
	BaseController
}

func (c *AllocationController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/allocation/index"
}

func (c *AllocationController) Index() {
	c.checkAuthor("allocation/index")

	if c.Ctx.Input.IsPost() {
		//获取数据列表和总数
		var params models.AllocationQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)

		var dep []interface{}
		for _,v := range c.curUser.ExtendDepartmentList {
			dep = append(dep, v)
		}
		params.Deps = dep

		data, total := models.AllocationPageList(&params)

		c.jsonDataTable(total, data)
	}

	c.display("", "allocation/index_footerjs.html")

}

//弹窗生成调拨资料
func (c *AllocationController) ChooseTrader() {
	c.checkAuthor("allocation/create")
	if c.Ctx.Input.IsPost() { //生成调拨表
		ser := services.AllocationFormData{}
		fromdep, _ := c.GetInt64("fromdep")
		targetdep, _ := c.GetInt64("targetdep")
		fromdep_name := c.GetString("fromdep_name")
		targetdep_name := c.GetString("targetdep_name")
		remarks := c.GetString("remarks")
		if id, err := ser.AllocationCreateRecords(&c.curUser, fromdep, targetdep, fromdep_name, targetdep_name, remarks); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", id)
		}
	}
	c.display("", "allocation/choosetrader_footerjs.html", "shared/layout_dialog.html")
}

func (c *AllocationController) Create() {
	c.checkAuthor("allocation/create")
	allocation_id, _ := c.GetInt64("allocation_id")
	if allocation_id == 0 {
		c.pageError("参数错误")
	}
	services.AllocationDetail(allocation_id)
	c.Data["allocation"] = models.GetAllocationById(allocation_id)

	c.showErpTypeid()
	c.display("", "allocation/create_footerjs.html")
}

//获取中间表数据
func (c *AllocationController) GetMiddleList() {
	allocation_id, _ := c.GetInt64("allocation_id")
	if allocation_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	list := models.AllocationMiddleList(allocation_id)
	c.jsonResult(enums.JRCodeSucc, "ok", list)
}

//删除明细表
func (c *AllocationController) DeleteMiddleData() {
	middle_id, _ := c.GetInt64("middle_id")
	if middle_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	if err := services.AllocationDeleteMiddleData(middle_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//选择指定门店库存
func (c *AllocationController) SelectErpInventory() {
	if c.Ctx.Input.IsPost() {
		allocation_id, _ := c.GetInt64("allocation_id")
		allocation := models.GetAllocationById(allocation_id)
		if allocation.Id == 0 {
			c.jsonDataTable(0, []interface{}{})
		}
		var qs models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.Department = allocation.FromDepId
		data, count := models.ErpInventoryPageList(&qs)
		c.jsonDataTable(count, data)
	}
}

//选择库存
func (c *AllocationController) SelectEntry() {
	c.checkAuthor("allocation/create")
	allocation_id, _ := c.GetInt64("allocation_id")
	products_id, _ := c.GetInt64("products_id")
	middle_id, _ := c.GetInt64("middle_id")

	c.Data["allocation_id"] = allocation_id
	c.Data["products_id"] = products_id
	c.Data["middle_id"] = middle_id

	//获取产品名称
	if product, err := models.GetErpProductsById(products_id, &c.curUser); err != nil {
		//c.pageError("找不到对应的产品信息")
	} else {
		c.Data["product"] = product
	}
	c.display("", "allocation/selectentry_footerjs.html", "shared/layout_dialog.html")
}

//选择库存
func (c *AllocationController) PopSelectEntry() {
	query := c.GetString("query")
	products_id, _ := c.GetInt64("product_id")
	c.Data["query"] = query
	c.Data["products_id"] = products_id
	c.display("", "allocation/popselectentry_footerjs.html", "shared/layout_dialog.html")
}

//输出二维表
func (c *AllocationController) Bivariatetable() {
	c.display("", "allocation/bivariatetable_footerjs.html", "shared/layout_dialog.html")
}

//重选光度范围
func (c *AllocationController) BivariatetableLen() {
	c.display("", "allocation/bivariatetablelen_footerjs.html", "shared/layout_dialog.html")
}

//按中间表获取明细
func (c *AllocationController) GetMiddleDataForEntry() {
	middle_id, _ := c.GetInt64("middle_id")
	ser := services.AllocationFormData{}
	if data, err := ser.GetMiddleDataForEntry(middle_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, "没有数据", "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}
}

//提交保存明细
func (c *AllocationController) SubmitOrderData() {
	data := c.GetString("data")
	if err := services.AllocationSubmitOrderData(data); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}
}

//提交调拨单据
func (c *AllocationController) SubmitOrder() {
	allocation_id, _ := c.GetInt64("allocation_id")
	if err := services.AllocationSubmitOrder(allocation_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

//查看盘点
func (c *AllocationController) View() {
	c.checkAuthor("allocation/index")

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}
	//获取明细数据
	if c.Ctx.Input.IsPost() {
		list := models.AllocationDataList(id)
		//获取价格
		paramsPrice := new(models.InventoryPriceQueryParams)
		for k, _ := range list {
			paramsPrice.DepId = list[k].Inventory.DepartmentId
			paramsPrice.Snd = list[k].Snd
			paramsPrice.Product = list[k].Products
			paramsPrice.Sph = list[k].Snd.Sph
			paramsPrice.Cyl = list[k].Snd.Cyl
			paramsPrice.Add = list[k].Snd.Add
			price := models.GetInventoryPriceForQueryParams(paramsPrice);
			if price.Id > 0 {
				list[k].Inventory.SuggestedPrice = price.SuggestedPrice
			}
		}
		c.jsonResult(enums.JRCodeSucc, "ok", list)
	}

	stock := models.GetAllocationById(id)
	if stock.Id == 0 {
		c.pageError("找不到指定的记录")
	}
	c.Data["m"] = stock
	c.display("", "allocation/view_footerjs.html")
}

//退回单据
func (c *AllocationController) ReturnAllocation() {
	allocation_id, _ := c.GetInt64("allocation_id")
	if err := services.ReturnAllocation(allocation_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

//确认调拨
func (c *AllocationController) ConfirmAllocation() {
	c.checkAuthor("allocation/confirm")
	allocation_id, _ := c.GetInt64("allocation_id")
	if err := services.ConfirmAllocation(allocation_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

//调拨接收入库
func (c *AllocationController) Warehousing() {
	c.checkAuthor("allocation/warehousing")

	if c.Ctx.Input.IsPost() {
		//获取数据列表和总数
		var params models.AllocationQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.ToDep = fmt.Sprintf("%d", c.curUser.DepartmentId)
		params.Auditing = []interface{}{5, 6}
		data, total := models.AllocationPageList(&params)
		c.jsonDataTable(total, data)
	}
	c.Data["activeSidebarUrl"] = "/allocation/warehousing"
	c.display("", "allocation/warehousing_footerjs.html")
}

//查看待确认调拨信息
func (c *AllocationController) WarehousingView() {
	c.checkAuthor("allocation/warehousing")
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}
	//获取明细数据
	if c.Ctx.Input.IsPost() {
		list := models.AllocationDataList(id)
		c.jsonResult(enums.JRCodeSucc, "ok", list)
	}

	stock := models.GetAllocationById(id)
	if stock.Id == 0 {
		c.pageError("找不到指定的记录")
	}
	if stock.ToDepId != c.curUser.DepartmentId {
		c.pageError("找不到指定的记录")
	}
	c.Data["m"] = stock
	c.Data["activeSidebarUrl"] = "/allocation/warehousing"
	c.display("", "allocation/warehousingview_footerjs.html")
}

//接收退回
func (c *AllocationController) WarehousingReturnAllocation() {
	allocation_id, _ := c.GetInt64("allocation_id")
	if err := services.WarehousingReturnAllocation(allocation_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

//确认接收
func (c *AllocationController) WarehousingConfirmAllocation() {
	allocation_id, _ := c.GetInt64("allocation_id")
	if err := services.WarehousingConfirmAllocation(allocation_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

//打印单据
func (c *AllocationController) Prints() {

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}
	stock, err := services.AllocationDetail(id)
	if err != nil {
		c.pageError("找不到指定的记录")
	}

	c.Data["m"] = stock

	c.display("", "allocation/prints_footerjs.html", "shared/layout_print.html")
}

//删除盘点
func (c *AllocationController) Delete() {
	c.checkAuthor("allocation/delete") //需要权限控制
	id, _ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "缺少参数", "")
	}

	err := services.AllocationDelete(id, c.curUser.DepartmentId)
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	c.jsonResult(enums.JRCodeSucc, "删除成功", "")

}
